package controllers;

import java.awt.event.ActionEvent;

import model.ButtonRequestModel;
import model.ComplaintReportModel;
import model.DateRequestModel;
import view.CEOviewGUI;
import view.ViewMonthlyGUI;
import view.ViewWeeklyGUI;
import view.ViewYearlyGUI;
import client.CliMessage;

/**
 * The Class CEOviewController that extends AbstractController.
 * @author yudku
 */
public class CEOviewController extends AbstractController
{	
	
	/** The CE oview view. */
	private CEOviewGUI CEOviewView;
	
	/** The Button request model. */
	private ButtonRequestModel ButtonRequestModel;
	
	/**
	 * This is produce CEOviewController constructor.
	 *
	 * @param CEOviewView is a produce report window GUI instance.
	 * @param ButtonRequestModel the button request model
	 */
	public CEOviewController(CEOviewGUI CEOviewView, ButtonRequestModel ButtonRequestModel) 
	{
		super (CEOviewView, ButtonRequestModel);
		this.CEOviewView = CEOviewView;
		this.ButtonRequestModel = ButtonRequestModel;

		this.CEOviewView.addWeekButtonListener(new WeekReportButtonListener(this));
		this.CEOviewView.addMonthButtonListener(new MonthReportButtonListener(this));
		this.CEOviewView.addYearButtonListener(new YearReportButtonListener(this));
	
	}
	
	/**
	 * Inner class that handles when WeekReportButtonListener Button Pressed by request Id, implements Action listener.
	 *
	 * @author Tal & Yuda
	 */
	
	class WeekReportButtonListener extends AbstractListener
	{
		
		/** The previus observer. */
		private CEOviewController previusObserver;
		
		/**
		 * Instantiates a new complaint report button listener.
		 *
		 * @param previusObserver the previus observer
		 */
		public WeekReportButtonListener (CEOviewController previusObserver)
		{	
			this.previusObserver = previusObserver;
		}

		@Override
		public void actionPerformed(ActionEvent arg0) 
		{
			ViewWeeklyGUI WeeklyView = new ViewWeeklyGUI();
			ComplaintReportModel reqModel = new ComplaintReportModel();
			
			ViewWeeklyController weeklyController = new ViewWeeklyController (WeeklyView, reqModel);
			
			setCurrentObserver(weeklyController);	
			
			
			setBackListener(previusObserver, WeeklyView);
			
			String searchQuery = "search"	+ "SELECT * "
                                            + "FROM weekly";
						                 
            sendToServer(searchQuery);				                       											
		}
	}
	
	
	/**
	 * The listener interface for receiving MonthReportButtonListener events.
	 *
	 * @see ArchiveReportButtonEvent
	 */
	class MonthReportButtonListener extends AbstractListener
	{
		
		/** The previus observer. */
		private CEOviewController previusObserver;
		
		/**
		 * Instantiates a new archive report button listener.
		 *
		 * @param previusObserver the previus observer
		 */
		public MonthReportButtonListener (CEOviewController previusObserver )
		{
			this.previusObserver = previusObserver;
	
		}

		@Override
		public void actionPerformed(ActionEvent arg0) 
		{
			ViewMonthlyGUI monthlyView = new ViewMonthlyGUI();
			DateRequestModel reqModel = new DateRequestModel();
			
			ViewMonthlyController monthController = new ViewMonthlyController (monthlyView, reqModel);
			
			setCurrentObserver(monthController);			
			setBackListener(previusObserver, monthlyView);

		}
	}

	/**
	 * The listener interface for receiving YearReportButtonListener events.
	 * @see PackageReportButtonEvent
	 */
	
	class YearReportButtonListener extends AbstractListener
	{
	
		/** The previus observer. */
		private CEOviewController previusObserver;
	
		
		/**
		 * Instantiates a new package report button listener.
		 *
		 * @param previusObserver the previus observer
		 */
		public YearReportButtonListener (CEOviewController previusObserver)
		{
			this.previusObserver = previusObserver;
	
		}
		@Override
		public void actionPerformed(ActionEvent arg0) 
		{
			
			ViewYearlyGUI YearlyView = new ViewYearlyGUI();
			ComplaintReportModel reqModel = new ComplaintReportModel();
			
			ViewYearlyController yearController = new ViewYearlyController (YearlyView, reqModel);
			
			setCurrentObserver(yearController);	
			
			
			setBackListener(previusObserver, YearlyView);
			
			String searchQuery = "search"	+ "SELECT * "
                                            + "FROM yearly";
						                 
            sendToServer(searchQuery);					                       											
		}
	}

	@Override
	public void handleDBRequestSuccess()
	{
	}
	
	@Override
	public void handleDBSearchResult(CliMessage dbResult) 
	{	
	}

	@Override
	public void handleNullSearchResult() 
	{
		//display main window
		
		CEOviewView.setVisible(true);
		CEOviewView.displayWrnMessage("NO DATA!");
	}
}
